﻿Imports System.IO
Imports Training.Common

Partial Public Class EditQuiz
    Inherits System.Web.UI.Page

    Dim qid As Integer ' quizid
    Dim qnm As String ' quizname
    Dim ppc As Integer ' passpercentage
    Dim blnNewImage As Boolean ' new image
    Dim oImage As String ' original image
    Dim uImage As String ' image to update

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        ' check for querystring data
        If IsNothing(Request.QueryString("qid")) Or IsNothing(Request.QueryString("qn")) Or IsNothing(Request.QueryString("pp")) Or IsNothing(Request.QueryString("ci")) Then
            Response.Redirect("QuizControl.aspx")
        Else
            qid = Request.QueryString("qid")
            qnm = Request.QueryString("qn")
            ppc = Request.QueryString("pp")
            oImage = Request.QueryString("ci")
        End If

        ' initial value for new image
        blnNewImage = False

        ' enable or disable image upload
        If Page.IsPostBack Then
            If cbImage.Checked Then
                EnableCert()
            Else
                DisableCert()
            End If
        Else
            LoadVariables()
        End If

        If blnNewImage Then
            lblMsg.Text = "Change Image"
        Else
            lblMsg.Text = "No Image Change"
        End If
    End Sub

    ''' <summary>
    ''' Load page variables into form
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub LoadVariables()
        tbQuizName.Text = qnm
        tbPass.Text = ppc
        lblImage.Text = oImage
    End Sub

    ''' <summary>
    ''' Enable the upload control
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub EnableCert()
        fuCert.Enabled = True
        rfFileUpload.Enabled = True
        blnNewImage = True
    End Sub

    ''' <summary>
    ''' Disable upload control
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub DisableCert()
        fuCert.Enabled = False
        rfFileUpload.Enabled = False
        blnNewImage = False
    End Sub

    ''' <summary>
    ''' Delete the quiz
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub btnDelete_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        ' sub to delete quiz
        DeleteQuiz(qid)
        lblMsg.Text = "Deleted QuizID: " & qid
    End Sub

    ''' <summary>
    ''' Update the quiz info
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub btnUpdate_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
        ' determine image name to update
        If cbImage.Checked Then
            uImage = UploadFile()
            Select Case uImage
                Case "empty"
                    lblMsg.Text = "Internal Error"
                    Exit Sub
                Case "wrongtype"
                    lblMsg.Text = "Wrong Image Type. bmp only."
                    Exit Sub
                Case "failed"
                    lblMsg.Text = "Upload failed."
                    Exit Sub
                Case "error"
                    lblMsg.Text = "An error occured."
                    Exit Sub
                Case Else
                    ' nothing, continue
            End Select
        Else
            uImage = oImage
        End If

        ' check if validation was successful
        If Page.IsValid Then
            ' update the info
            If UpdateQuizInfo(qid, tbQuizName.Text, tbPass.Text, uImage) = 0 Then
                ' successful update
                lblMsg.Text = "Quiz: " & tbQuizName.Text & " was successfully updated."
            Else
                ' update failed
                lblMsg.Text = "Update of Quiz information failed."
            End If
        Else
            lblMsg.Text = "Validation failed. Update aborted."
        End If

    End Sub

    ''' <summary>
    ''' Upload file for cert image and add quiz data
    ''' </summary>
    ''' <remarks></remarks>
    Private Function UploadFile() As String
        Dim fn As String ' file name
        Dim ct As String ' file content
        Dim fz As String ' file size
        Dim fileUploaded As Boolean
        Dim retVal As String

        retVal = "empty"

        ' initial value of fileUploaded
        fileUploaded = False

        fn = fuCert.PostedFile.FileName
        ct = fuCert.PostedFile.ContentType
        fz = fuCert.PostedFile.ContentLength

        ' define path to upload folder
        Dim p As String
        p = Server.MapPath("../images")

        Try
            ' upload the actual file
            fuCert.PostedFile.SaveAs(p & "\" & System.IO.Path.GetFileName(fn.ToString()))
            ' set fileUploaded
            fileUploaded = True

            ' proceed with data insert if upload was successful
            If fileUploaded Then
                ' get file type and path
                Dim typ As String ' type of file
                Dim pth As String ' path to file
                typ = Right(fn, 4)
                pth = System.IO.Path.GetFileName(fn.ToString())

                ' determine type
                Select Case typ
                    Case ".bmp"
                        typ = ".bmp"
                        retVal = pth
                    Case Else
                        retVal = "wrongtype"
                End Select
            Else
                ' upload failed, notify user
                retVal = "failed"
            End If
        Catch ex As Exception
            ' also log error here
            retVal = "error"
        End Try

        Return retVal
    End Function
End Class